﻿#include"HashTable.h"

void test_set1()
{
	unordered_set<int> s = { 3,1,6,7,8,2 };
	unordered_set <int>::iterator it = s.begin();
	while (it != s.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;
}

int test_set2()
{
	const size_t N = 1000000;
	unordered_set<int> us;
	set<int> s;
	vector<int> v;
	v.reserve(N);
	srand(time(0));

	for (size_t i = 0; i < N; ++i)
	{
		//v.push_back(rand()); // N⽐较⼤时，重复值⽐较多
		v.push_back(rand() + i); // 重复值相对少
		//v.push_back(i); // 没有重复，有序
	}

	size_t begin1 = clock();

	for (auto e : v)
	{
		s.insert(e);
	}

	size_t end1 = clock();
	cout << "set insert:" << end1 - begin1 << endl;

	size_t begin2 = clock();
	us.reserve(N);

	for (auto e : v)
	{
		us.insert(e);
	}
	size_t end2 = clock();
	cout << "unordered_set insert:" << end2 - begin2 << endl;

	int m1 = 0;
	size_t begin3 = clock();
	for (auto e : v)
	{
		auto ret = s.find(e);
		if (ret != s.end())
		{
			++m1;
		}
	}
	size_t end3 = clock();
	cout << "set find:" << end3 - begin3 << "->" << m1 << endl;

	int m2 = 0;
	size_t begin4 = clock();
	for (auto e : v)
	{
		auto ret = us.find(e);
		if (ret != us.end())
		{
			++m2;
		}
	}
	size_t end4 = clock();

	cout << "unorered_set find:" << end4 - begin4 << "->" << m2 << endl;
	cout << "插入数据个数：" << s.size() << endl;
	cout << "插入数据个数：" << us.size() << endl;

	size_t begin5 = clock();
	for (auto e : v)
	{
		s.erase(e);
	}
	size_t end5 = clock();
	cout << "set erase:" << end5 - begin5 << endl;

	size_t begin6 = clock();
	for (auto e : v)
	{
		us.erase(e);
	}
	size_t end6 = clock();
	cout << "unordered_set erase:" << end6 - begin6 << endl << endl;

	return 0;
}

//int main()
//{
//	//test_set1();
//	test_set2();
//
//	return 0;
//}




struct Date
{
	int _year;
	int _month;
	int _day;

	Date(int year=1,int month=1,int day=1)
		:_year(year)
		,_month(month)
		,_day(day)
	{}
	bool operator==(const Date& d)
	{
		return _year == d._year
			&& _month == d._month
			&& _day == d._day;
	}
};
struct DateHashFunc
{
	size_t operator()(const Date& d)
	{
		size_t hash = 0;
		hash += d._year;
		hash *= 131;

		hash += d._month;
		hash *= 131;

		hash += d._day;
		hash *= 131;


		return hash;
	}
};

//int main()
//{
//	int a[] = { 19, 30,5,36,13,20,21,12};
//	open_address::HashTable<int, int> ht;
//	for (auto e : a)
//	{
//		ht.Insert({ e,e });
//	}
//	ht.Insert({ 15,15 });
//
//	ht.Erase(30);
//	if (ht.Find(20))
//	{
//		cout << "找到了"  << endl;
//	}
//	else
//	{
//		cout << "没有" << endl;
//	}
//
//	const char* a2[] = { "abcd","sort","insert" };
//	open_address::HashTable<string, string> ht2;
//	for (auto e : a2)
//	{
//		ht2.Insert({ e,e });
//	}
//	
//	open_address::HashTable<Date,int,DateHashFunc> ht3;
//	ht3.Insert({ { 2025,2,27 },1 });
//	ht3.Insert({ { 2025,12,7 },1 });
//
//	return 0;
//}


int main()
{
	int a[] = { 19,30,5,36,13,20,21,12,24,96 };
	hash_bucket::HashTable<int, int> ht;
	for (auto e : a)
	{
		ht.Insert({ e,e });
	}
	//ht.Insert({ 100,100 });
	//ht.Insert({ 101,101 });

	cout << ht.Find(96) << endl;
	cout << ht.Find(30) << endl;
	cout << ht.Find(19) << endl;

	ht.Erase(96);
	ht.Erase(19);
	ht.Erase(30);

	cout << ht.Find(96) << endl;
	cout << ht.Find(30) << endl;
	cout << ht.Find(19) << endl;

	const char* a2[] = { "abcd","sort","insert" };
		hash_bucket::HashTable<string, string> ht2;
		for (auto e : a2)
		{
			ht2.Insert({ e,e });
		}

	return 0;
}